package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.dto.QueryPageBeanDTO;
import com.itheima.common.dto.SetmealDTO;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.SetmealCheckGroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealData;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealCheckGroupMapper setmealCheckGroupMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加套餐
     * @param setmeal
     * @param checkGroupIds
     */
    @Override
    public void addSetmeal(Setmeal setmeal,String checkGroupIds) {
        setmealMapper.insert(setmeal);
        Integer id = setmeal.getId();

        if(checkGroupIds != null && checkGroupIds.length() > 0) {
            String[] checkgroupIds = checkGroupIds.split(",");
            for (String checkGroupId : checkgroupIds) {
                setmealCheckGroupMapper.insert(id, Integer.parseInt(checkGroupId));
            }
        }

        //将新增套餐的图片url保存到redis
        redisTemplate.opsForSet().add("savedFile",setmeal.getImg());
    }

    /**
     * 删除套餐
     * @param id
     */
    @Override
    public void deleteSetmeal(Integer id) {
        Setmeal setmeal = setmealMapper.getSetmealById(id);
        setmealMapper.deleteSetmealById(id);
        setmealCheckGroupMapper.deleteBySetmealId(id);
        redisTemplate.opsForSet().remove("savedFile",setmeal.getImg());
    }

    /**
     * 修改套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void modifySetmeal(Setmeal setmeal,String checkgroupIds) {
        setmealCheckGroupMapper.deleteBySetmealId(setmeal.getId());
        Setmeal setmealtemp = setmealMapper.getSetmealById(setmeal.getId());
        redisTemplate.opsForSet().remove("savedFile",setmealtemp.getImg());
        setmealMapper.update(setmeal);
        String[] checkGroups = checkgroupIds.split(",");
        for (String checkGroup : checkGroups) {
            setmealCheckGroupMapper.insert(setmeal.getId(),Integer.parseInt(checkGroup));
        }
        redisTemplate.opsForSet().add("savedFile",setmeal.getImg());
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @Override
    public Setmeal getSetmealById(Integer id) {
        Setmeal setmeal = setmealMapper.getSetmealById(id);
//        List<Integer> checkGroupIds = setmealCheckGroupMapper.getCheckGroupIdsBySetmealId(id);
//        if(checkGroupIds != null){
//            setmeal.setCheckGroups(checkGroupIds.toString());
//        }
        return setmeal;
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageSetmeal(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> p = setmealMapper.pageSetmeal(queryPageBean);
        List<Setmeal> result = p.getResult();
//        for (SetmealDTO setmealDTO : result) {
//            Integer id = setmealDTO.getId();
//            List<Integer> checkGroupIds = setmealCheckGroupMapper.getCheckGroupIdsBySetmealId(id);
//            setmealDTO.setCheckGroups(checkGroupIds.toString());
//        }
        return new PageResult(p.getTotal(),result);
    }

    /**
     * 获取所有套餐数据
     * @return
     */
    @Override
    public List<Setmeal> getData() {
        return setmealMapper.getData();
    }

    /**
     * 根据套餐id查询关联的检查组
     * @param id
     * @return
     */
    @Override
    public List<CheckGroup> getCheckGroupsBySetmealId(Integer id) {
        return setmealCheckGroupMapper.getCheckGroupsBySetmealId(id);
    }
}
